package com.ruoyi.lab.utils;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 付疆疆
 * @date 2020/9/2 15:29
 */
public class TimeTool {
    //"2020-09-02 15:51:34"
    //日期转化字符串yyyy-MM-dd HH:mm:ss Wed Sep 02 00:00:00 CST 2020
    public static String DateToString(Date arg1){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(arg1);
        return dateString;
    }
    public static Long DateToLong(Date arg1){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long dateString = Long.parseLong(formatter.format(arg1));
        return dateString;
    }
    //日期转字符串yyyy-MM-dd
    public static String DateToStringDay(Date arg1){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(arg1);
        return dateString;
    }


    //字符串转化日期yyyy-MM-dd HH:mm:ss Wed Sep 02 00:00:00 CST 2020 两者时间格式都可以
    public static Date StringToDate(String arg1) throws ParseException  {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //DateFormat format2= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        Date date = format.parse(arg1);
        return date;
    }
    
    //字符串转化日期yyyy-MM-dd HH:mm:ss Wed Sep 02 00:00:00 CST 2020 两者时间格式都可以
    public static Date yyyyMMddStringToDate(String arg1) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        //DateFormat format2= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        Date date = format.parse(arg1);
        return date;
    }

    //字符串转时间戳
    public static String StringToStamp(String arg1) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = String.valueOf(formatter.parse(arg1).getTime()/1000);
        return str;
    }

    public static Long dateToStamp(String str) throws Exception {
//        String str = "2019-03-13 13:54:00";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(str);
        long ts = date.getTime();
        return ts;
    }

    public static Long dateToStampSimple(String str) throws Exception {
//        String str = "2019-03-13 13:54:00";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(str);
        long ts = date.getTime();
        return ts;
    }
    //时间戳转化为字符串(时间戳就是字符串)


    public static void main(String[] args) throws Exception {
        Date currentTime = new Date();
        System.out.println(TimeTool.DateToString(currentTime));
        System.out.println(TimeTool.DateToStringDay(currentTime));

        System.out.println(StringToDate("2020-09-02"));
        System.out.println(StringToStamp("2020-09-02 15:51:34"));
        System.out.println(TimeTool.dateToStamp("2020-09-02 15:51:34"));
        System.out.println(TimeTool.dateToStampSimple("2020-09-02"));

        System.out.println(StringToStamp(TimeTool.DateToString(currentTime)));
    }
    
    /**
     * 字符串时间与当前时间比较大小 
     * @param time
     * @return  0 小于当前时间，1 等当前时间，2 大于当前时间，-1 其它
     */
    @SuppressWarnings("unused")
	public static int toCompareTime(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String newTimes = sdf.format(new Date());
            Date newTime = sdf.parse(newTimes);
            Date sTime = sdf.parse(time);
            if (sTime.getTime()<newTime.getTime())return 0;
            if (sTime.getTime()==newTime.getTime())return 1;
            if (sTime.getTime()>newTime.getTime())return 2;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;

}
}
